package com.taotao.service.Impl;

import com.taotao.bean.TbContent;
import com.taotao.bean.TbContentCategory;
import com.taotao.mapper.TbContentMapper;
import com.taotao.service.ContentService;
import com.taotao.utils.FileUploadUtils;
import com.taotao.utils.IDUtils;
import com.taotao.utils.TaotaoResult;
import com.taotao.vo.LayuiEditResult;
import com.taotao.vo.LayuiTableResult;
import com.taotao.vo.PicData;
import com.taotao.vo.ZtreeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper contentMapper;

    @Override
    public List<ZtreeResult> findContentCategoryByParentId(Long parentId) {
        List<TbContentCategory> tbContentCategoryList = contentMapper.findContentCategoryByParentId(parentId);
        List<ZtreeResult> results = new ArrayList<ZtreeResult>();
        for (TbContentCategory category : tbContentCategoryList) {
            ZtreeResult result = new ZtreeResult();
            result.setId(category.getId());
            result.setName(category.getName());
            result.setIsParent(category.getIsParent());
            results.add(result);
        }
        return results;
    }

    @Override
    public TaotaoResult updateContentCategory(TbContentCategory tbContentCategory) {
        int count = contentMapper.updateContentCategory(tbContentCategory);
        if (count == 0) {
            return TaotaoResult.build(500, "内容分类修改不成功");
        }
        return TaotaoResult.build(200, "内容分类修改成功");
    }

    @Override
    public TaotaoResult deleteContentCategory(TbContentCategory tbContentCategory) {
        if (tbContentCategory.getIsParent()) {
            //如果你是父节点你还要删除子节点
            int count = contentMapper.deleteContentCategoryByParent(tbContentCategory);
            if (count == 0) {
                return TaotaoResult.build(500, "删除内容分类错误");
            }
        }
        //不管你是不是父节点 都要删除自己本身
        int count = contentMapper.deleteContentCategoryById(tbContentCategory);
        if (count == 0) {
            return TaotaoResult.build(500, "删除内容分类错误");
        }

        return TaotaoResult.build(200, "删除内容分类成功");
    }

    @Override
    public TaotaoResult addContentCategory(TbContentCategory tbContentCategory) {
        //我们要修改的是
        int count = contentMapper.updateContentCategory(tbContentCategory);
        if (count == 0) {
            return TaotaoResult.build(500, "添加失败");
        }
        Date date = new Date();
        tbContentCategory.setParentId(tbContentCategory.getId());
        //因为主键自增长
        tbContentCategory.setId(null);
        tbContentCategory.setName("新增节点");
        tbContentCategory.setStatus(1);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        tbContentCategory.setCreated(date);
        tbContentCategory.setUpdated(date);
        //数据装载完毕 开始添加数据
        int count2 = contentMapper.addContentCategory(tbContentCategory);
        if (count2 == 0) {
            return TaotaoResult.build(500, "添加失败");
        }
        return TaotaoResult.build(200, "添加成功");
    }

    @Override
    public LayuiTableResult findContentByCId(Long categoryId) {
        LayuiTableResult result = new LayuiTableResult();
        result.setCode(0);
        result.setMsg("");
        int count = contentMapper.findContentByCount(categoryId);
        result.setCount(count);
        List<TbContent> data = contentMapper.findContentByCId(categoryId, 0, 10);
        result.setData(data);
        return result;
    }

    @Override
    public LayuiTableResult findContentByCategoryId(Long categoryId, Integer limit, Integer page) {
        LayuiTableResult result = new LayuiTableResult();
        result.setCode(0);
        result.setMsg("");
        int count = contentMapper.findContentByCount(categoryId);
        result.setCount(count);
        List<TbContent> data = contentMapper.findContentByCId(categoryId, (page - 1) * limit, limit);
        result.setData(data);
        return result;
    }

    @Override
    public TaotaoResult deleteContent(List<TbContent> tbContents) {
        List<Long> ids = new ArrayList<Long>();
        for (TbContent tbContent : tbContents) {
            ids.add(tbContent.getId());
        }
        int count = contentMapper.deleteContent(ids);
        if (count == 0) {
            return TaotaoResult.build(500, "删除失败");
        }

        return TaotaoResult.build(200, "删除成功", tbContents.get(0).getCategoryId());
    }

    @Override
    public LayuiEditResult addPic(MultipartFile file) {
        List<Object> uploderFile = FileUploadUtils.getUploderFile(file);
        try {
            //就会把图片存入到我们的E盘up文件夹里面
            file.transferTo(new File((File) uploderFile.get(1), (String) uploderFile.get(2)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        LayuiEditResult result = new LayuiEditResult();
        result.setCode(0);
        result.setMsg("");
        PicData data = new PicData();
        data.setSrc("http://localhost/" + uploderFile.get(0) + "/" + uploderFile.get(2));
        data.setTitle((String) uploderFile.get(2));
        result.setData(data);
        return result;
    }

    @Override
    public TaotaoResult addContent(TbContent tbContent) {
        Date date = new Date();
        tbContent.setCreated(date);
        tbContent.setUpdated(date);
        int count = contentMapper.addContent(tbContent);
        if (count == 0) {
            return TaotaoResult.build(500, "添加失败");
        }
        return TaotaoResult.build(500, "添加成功");
    }
}
